package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.aop.annotation.LogAnno;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result getAllCourses(
            @RequestParam(name="page",required = true,defaultValue = "1")Integer page,
            @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize,
            @RequestParam(name="name",required = false) String name,
            @RequestParam(name="subject",required = false) Integer subject,
            @RequestParam(name="target",required = false) Integer target
    ){
        PageResult<Course> PageResult=courseService.page(page, pageSize, name, subject, target);
        return Result.success(PageResult);
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        log.info("删除课程: {}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    @LogAnno
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer courseId){
        log.info("查询课程: {}",courseId);
        Course course = courseService.getById(courseId);
        return Result.success(course);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程: {}",course);
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Course> pageResult = courseService.findAll();
        return Result.success(pageResult);
    }

    @GetMapping("/subject/{subject}")
    public Result findBySubject(@PathVariable("subject") Integer subject){
        List<Course> pageResult = courseService.findBySubject(subject);
        return Result.success(pageResult);
    }
}
